package testing;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import model.MP3File;
import model.MP3FileList;
import model.MP3FileMutableTreeNode;

import org.junit.Before;
import org.junit.Test;

import controller.ID3Parser;
import controller.ID3Writer;
import controller.XMLCache;

public class CacheTest {

	MP3File testFile = null;
	File newFile;
	String newTitle= "newTitle";

	@Before
	public void init() {
		File someFile = Paths.get("./bin/testing/01Turtle.mp3").toFile();
		System.out.println(someFile.getAbsolutePath());
		System.out.println(someFile.isFile());
		
		newFile = new File("./bin/testing/test.mp3");
		try {
			Files.copy(someFile.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void test() {
		MP3FileList list = new MP3FileList("./bin/testing/");
		list.addFile(new MP3FileMutableTreeNode("./bin/testing/test.mp3", "test"));
		XMLCache.createCache(list.getPath(), list);
	
		MP3FileList cacheList = XMLCache.readCache("./bin/testing/");

		assertEquals(list.getFileList().get(0).getMP3File().getTitle(), cacheList.getFileList().get(0).getMP3File().getTitle());
		assertEquals(list.getFileList().get(0).getMP3File().getAlbum(), cacheList.getFileList().get(0).getMP3File().getAlbum());
		assertEquals(list.getFileList().get(0).getMP3File().getArtist(), cacheList.getFileList().get(0).getMP3File().getArtist());
		assertEquals(list.getFileList().get(0).getMP3File().getYear(), cacheList.getFileList().get(0).getMP3File().getYear());
		
		System.out.println("fertig");
	}
	
	@Test
	public void testCacheNoDirectoryChanges() {
		MP3FileList list = new MP3FileList("./bin/testing/");
		list.addFile(new MP3FileMutableTreeNode("./bin/testing/test.mp3", "test"));
		XMLCache.createCache(list.getPath(), list);
	
		assertFalse("Cache has to be changed, but there where no changes within its Directory", XMLCache.hasToBeCached(list.getPath()));
	}
	
	@Test
	public void testCacheDirectoryTimeStampChanges() {
		MP3FileList list = new MP3FileList("./bin/testing/");
		list.addFile(new MP3FileMutableTreeNode("./bin/testing/test.mp3", "test"));
		XMLCache.createCache(list.getPath(), list);

		File folder = new File("./bin/testing/");
		folder.setLastModified(System.currentTimeMillis()+1000);
		
		assertTrue("Cache was not changed, but there where changes within its Directory", XMLCache.hasToBeCached(list.getPath()));
	}
	
	@Test
	public void testCacheDirectoryCacheValueAfterFileChanges() {
		MP3FileList list = new MP3FileList("./bin/testing/");
		list.addFile(new MP3FileMutableTreeNode("./bin/testing/test.mp3", "test"));
		XMLCache.createCache(list.getPath(), list);
		MP3FileList cacheList = XMLCache.readCache("./bin/testing/");
		assertEquals(list.getFileList().get(0).getMP3File().getTitle(), cacheList.getFileList().get(0).getMP3File().getTitle());
		
		MP3File testFile = null;
		try {
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	
		
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TIT2", newTitle);
			ID3Writer.writeMP3ToFile(testFile);
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}

		MP3FileList list2 = new MP3FileList("./bin/testing/");
		list2.addFile(new MP3FileMutableTreeNode("./bin/testing/test.mp3", "test"));
		XMLCache.createCache(list.getPath(), list2);
		cacheList = XMLCache.readCache("./bin/testing/");
		assertEquals("2",newTitle, cacheList.getFileList().get(0).getMP3File().getTitle());
	}
}
